<template>
	<view class="content">
		<view class="wid100 bacFFF">
			<view style="height: 30rpx;"></view>
			<view class="wid90 mar dis disAl disJuB">
				<view class="foSi30">头像</view>
				<view class="dis disAl" @click="shangImage">
					<view class="mar-right20" style="width: 98rpx;height: 98rpx;">
						<image v-if="!image" src="../../static/tu3.png" class="wh100 borRad50"></image>
						<image v-if="image && imin == 0" :src="url+image" class="wh100 borRad50"></image>
						<image v-if="image && imin == 1" :src="image" class="wh100 borRad50"></image>
					</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
			</view>
			<view class="wid90 mar mar-top30 hei80 dis disAl disJuB">
				<view class="foSi30">昵称</view>
				<view class="dis disAl wid70" style="justify-content: flex-end;" @click="open1">
					<view class="foSi30 mar-right20 overflowHidden" style="color: #737373;">{{name}}</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
			</view>
			<view class="wid90 mar hei80 dis disAl disJuB">
				<view class="foSi30">手机号</view>
				<view class="dis disAl" @click="open">
					<view class="foSi30 mar-right20" style="text-align: right;" :style="phone?'':'color: #737373;'">
						{{phone?phone:'绑定手机号'}}</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
			</view>
			<view class="wid90 mar hei80 dis disAl disJuB">
				<view class="foSi30">注册时间</view>
				<view class="foSi30" style="text-align: right;color: #737373;">{{createtime}}</view>
			</view>
			<view style="height: 50rpx;"></view>
		</view>
		<view class="mar-top20 bacFFF" style="padding: 20rpx 0;">
			<view class="wid90 mar">
				<view class="hei80 dis disAl disJuB" @click="tiaozhuan('/pages/common/richtext?type=1')">
					<view class="foSi30">隐私政策</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
				<view class="hei80 dis disAl disJuB" @click="tiaozhuan('/pages/common/richtext?type=2')">
					<view class="foSi30">服务协议</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
				<view class="hei80 dis disAl disJuB" @click="tiaozhuan('/pages/wode/shield')">
					<view class="foSi30">屏蔽列表</view>
					<uni-icons type="right" size="30rpx"></uni-icons>
				</view>
				<view class="hei80 dis disAl disJuB">
					<view class="foSi30">个性化推荐</view>
					<switch :checked='tujian' :color="theme.main_color" style="transform:scale(0.7)"
						@click="tujianing" />
				</view>
			</view>
		</view>
		<view class="poFix wid100 dis disAl disJuC" style="height: 124rpx;bottom: 0;">
			<view class="wid90 borRad40 dis coFFF foSi35 hei86" style="overflow: hidden;">
				<view class="hei100 dis disAl disJuC" style="width: 50%;"
					:style="'background-color: '+theme.seco_color+';'" @click="outlogin(2)">注销账号</view>
				<view class="hei100 dis disAl disJuC" @click="outlogin(1)" style="width: 50%;"
					:style="'background-color: '+theme.main_color+';'">退出登录</view>
			</view>
		</view>
		<uni-popup ref="popup1" type="center">
			<view class="borRad20 bacFFF" style="width: 596rpx;padding: 30rpx 0;">
				<view class="wid90 mar">
					<view class="borRad20 dis disAl disJuC" style="height: 90rpx;background-color: #EEEEEE;">
						<input class="wid90 foSi30" v-model="name" placeholder="请输入昵称" />
					</view>
					<view class="mar-top40 hei80 borRad40 dis disAl disJuC coFFF mar foSi30" @click="baocun(0)"
						style="width: 50%;" :style="'background-color: '+theme.main_color+';'">保存</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup" type="center">
			<view class="borRad20 bacFFF" style="width: 596rpx;padding: 30rpx 0;">
				<view class="wid90 mar">
					<view class="borRad20 dis disAl disJuC" style="height: 90rpx;background-color: #EEEEEE;">
						<input class="wid90 foSi30" v-model="mobile" type="number" placeholder="请输入手机号" />
					</view>
					<view class="borRad20 dis disAl disJuC mar-top30" style="height: 90rpx;background-color: #EEEEEE;">
						<input style="width: 65%;" class="foSi30" v-model="code" placeholder="请输入验证码" />
						<view v-if="sec == 0" @click="getCheckNum()" class="dis disAl disJuC wid25 foSi25"
							style="color: #9D9D9D;">获取验证码</view>
						<view v-if="sec > 0" class="dis disAl disJuC wid25" style="color: #9D9D9D;">{{sec}}</view>
					</view>
					<view class="mar-top40 hei80 borRad40 dis disAl disJuC coFFF mar foSi30" @click="baocunMobile()"
						style="width: 50%;" :style="'background-color: '+theme.main_color+';'">保存</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				name: '',
				phone: '',
				tujian: true,
				xuan: 1, // 1=tuijian true 2=tuijian false
				image: '',
				createtime: '',
				mobile: '',
				code: '',
				sec: 0, //这是重新获取验证码的倒计时
				imin: 1,
				theme: null
			}
		},
		onLoad() {
			var that = this
			that.getUser()
			that.theme = uni.getStorageSync('theme')
			console.log(that.tujian);
			console.log(uni.getStorageSync('tujian'));
			that.tujian = uni.getStorageSync('tujian') == 1 ? true : false
			console.log(that.tujian);
		},
		methods: {
			tujianing() {
				if (this.tujian == true) {
					uni.setStorageSync('tujian', 2)
					this.tujian = false
					uni.showToast({
						title: '已关闭',
						icon: 'none',
						duration: 2000
					})
				} else {
					uni.setStorageSync('tujian', 1)
					this.tujian = true
					uni.showToast({
						title: '已开启',
						icon: 'none',
						duration: 2000
					})
				}
			},
			getCheckNum() {
				var that = this;
				// 初始化定时器
				let t = null;
				// 倒计时1分钟
				this.sec = 60
				that.getCode()
				// 计时器清零
				clearInterval(t);
				// that.sendcode()
				t = setInterval(() => {
					// 当倒计时完时，显示重新发送
					if (this.sec == 0) {
						clearInterval(t);
						return;
					}
					// 实现倒计时效果
					this.sec--;
				}, 1000)

			},
			getCode() {
				var that = this;
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/common/send_code',
					method: "POST",
					header: {
						'site-id': that.global.site_id //自定义请求头信息
					},
					data: {
						mobile: that.mobile
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							uni.showToast({
								title: result.msg,
								//显示持续时间为 2秒
								duration: 2000
							})
						} else {
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					}
				})
			},
			getUser() {
				var that = this
				uni.showLoading({
					title: '加载中...'
				})
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/user/userinfo',
					method: 'POST',
					header: {
						'site-id': that.global.site_id,
						'token': uni.getStorageSync('token')
					},
					data: {},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.name = result.data.nickname
							that.phone = result.data.mobile
							that.image = result.data.avatar
							that.createtime = result.data.createtime
						} else {
							uni.showToast({
								title: result.msg,
								icon: 'none',
								duration: 2000
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			baocun(index) {
				var that = this;
				var data = {
					nickname: that.name
				}
				if (index == 1) {
					data = {
						avatar: that.image
					}
				}
				if (!that.name && index != 1) {
					return uni.showToast({
						title: '请输入昵称',
						icon: 'error',
						duration: 2000
					})
				};
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/user/upuser',
					method: "POST",
					header: {
						'site-id': that.global.site_id, //自定义请求头信息
						'token': uni.getStorageSync('token')
					},
					data: data,
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.$refs.popup1.close();
							uni.navigateBack(1)
						} else {
							uni.showToast({
								title: result.msg,
								//将值设置为 success 或者直接不用写icon这个参数
								icon: 'error',
								//显示持续时间为 2秒
								duration: 2000
							})
						}
					}
				})

			},
			open() {
				this.$refs.popup.open()
			},
			open1() {
				this.$refs.popup1.open()
			},
			baocunMobile() {
				var that = this;
				//正则表达式定义手机号正确格式
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
				if (!reg.test(that.mobile)) {
					return uni.showToast({
						title: '请输入正确手机号',
						icon: 'error',
						duration: 2000
					})
				};
				if (!that.code) {
					return uni.showToast({
						title: '请输入验证码',
						icon: 'error',
						duration: 2000
					})
				};
				uni.request({
					url: that.global.url + 'api/qianyu_smfw/user/upmobile',
					method: "POST",
					header: {
						'site-id': that.global.site_id, //自定义请求头信息
						'token': uni.getStorageSync('token')
					},
					data: {
						mobile: that.mobile,
						code: that.code
					},
					success(res) {
						let result = res.data
						if (result.code == 1) {
							that.phone = that.mobile
							that.$refs.popup.close()
						} else {
							if (result.msg == '手机号码重复') {
								// 询问是否同步账号
								uni.showModal({
									title: '提示',
									content: '该手机号码已注册，是否同步当前账号？',
									success: function(res) {
										if (res.confirm) {
											// 执行确认后的操作
											uni.request({
												url: that.global.url + 'api/qianyu_smfw/user/account_synchronization',
												method: "POST",
												header: {
													'site-id': that.global.site_id, //自定义请求头信息
													'token': uni.getStorageSync('token')
												},
												data: {
													mobile: that.mobile
												},
												success(res) {
													let result = res.data
													console.log(result)
													if (result.code == 1) {
														uni.setStorageSync('token',result.data)
														uni.showToast({
															title: '同步成功',
															//将值设置为 success 或者直接不用写icon这个参数
															icon: 'error',
															//显示持续时间为 2秒
															duration: 2000
														})
														setTimeout(()=>{
															uni.switchTab({
																url:'/pages/index/index'
															})
														},1000)
													} else {
														uni.showToast({
															title: result.msg,
															//将值设置为 success 或者直接不用写icon这个参数
															icon: 'error',
															//显示持续时间为 2秒
															duration: 2000
														})
													}
												}
											})
										} else {
											// 执行取消后的操作
											uni.showToast({
												title: result.msg,
												//将值设置为 success 或者直接不用写icon这个参数
												icon: 'error',
												//显示持续时间为 2秒
												duration: 2000
											}) 
										}
									}
								})
							}else{
								uni.showToast({
									title: result.msg,
									//将值设置为 success 或者直接不用写icon这个参数
									icon: 'error',
									//显示持续时间为 2秒
									duration: 2000
								}) 
							}
							
						}
					}
				})

			},
			tiaozhuan(url) {
				uni.navigateTo({
					url: url
				})
			},
			outlogin(type) {
				let tips = '确认要退出吗?'
				if(type == 2){
					tips = '确认要注销账户吗？'
				}
				uni.showModal({
					title: '温馨提示',
					content: tips,
					success: function(res) {
						if (res.confirm) {
							// 存用户信息
							uni.removeStorageSync('token')
							uni.removeStorageSync('code')
							console.log(uni.getStorageSync('token'))
							uni.setStorageSync('cdx', 1)
							uni.setStorageSync('duan', 0);
							uni.switchTab({
								url: '/pages/wode/wode'
							})
						}
					}
				});
			},
			//上传图片
			shangImage() {
				var that = this
				// #ifdef APP
				var xiangce = 0;
				var xiangji = 0;
				const permission = 'android.permission.WRITE_EXTERNAL_STORAGE'; // 要检查的权限
				const android_camera = 'android.permission.CAMERA';
				plus.android.checkPermission(
					permission,
					granted => {
						console.log(granted);
						if (granted.checkResult == '-1') {
							console.log('相册未授权');
							xiangce = 1;
						}
					},
					error => {
						console.error('Error checking permission:', error.message);
					}
				);
				plus.android.checkPermission(
					android_camera,
					granted => {
						console.log(granted);
						if (granted.checkResult == '-1') {
							console.log('相机未授权');
							xiangji = 1;
						}
					},
					error => {
						console.error('Error checking permission:', error.message);
					}
				);
				setTimeout(function() {
					console.log(xiangce);
					console.log(xiangji);
					if (xiangce == 1 && xiangji == 1) {
						uni.showModal({
							title: '提示',
							content: that.global.name +
								'对存储空间/照片/相机/摄像头权限申请说明：便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、举报用户、保存相册、发布动态、下载与客服沟通等场景中读取和写入相册和文件内容。',
							success: function(res) {
								if (res.confirm) {
									that.picopsshow = true;
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							},
						})
					} else if (xiangce == 1 && xiangji == 0) {
						uni.showModal({
							title: '提示',
							content: that.global.name +
								'对存储空间/照片/相机/摄像头权限申请说明：便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、举报用户、保存相册、发布动态、下载与客服沟通等场景中读取和写入相册和文件内容。',
							success: function(res) {
								if (res.confirm) {
									that.picopsshow = true;
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							},
						})
					} else if (xiangce == 0 && xiangji == 1) {
						uni.showModal({
							title: '提示',
							content: that.global.name +
								'对存储空间/照片/相机/摄像头权限申请说明：便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、举报用户、保存相册、发布动态、下载与客服沟通等场景中读取和写入相册和文件内容。',
							success: function(res) {
								if (res.confirm) {
									that.picopsshow = true;
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							},
						})
					} else {
						that.picopsshow = true;
					}
				}, 100)
				// #endif
				uni.chooseImage({
					count: 1,
					success: (res) => {
						that.shangchuan(res.tempFilePaths[0])
					},
				})
			},
			shangchuan(tempFilePaths) {
				var that = this
				uni.uploadFile({
					url: that.global.url + 'api/qianyu_smfw/common/file_upload',
					header: {
						'site-id': that.global.site_id,
						"token": uni.getStorageSync('token'),
					}, //请求token	
					filePath: tempFilePaths,
					name: 'file',
					success: (res) => {
						var apiresult = JSON.parse(res.data)
						if (apiresult.code == 1) {
							console.log(apiresult.data);
							that.image = apiresult.data;
							that.imin = 0
							that.baocun(1)
						} else {
							uni.showToast({
								title: apiresult.msg,
								icon: 'error',
								duration: 2000
							})
						}
					},
					fail() {
						uni.showToast({
							title: "网络连接失败",
							icon: 'error',
							duration: 2000
						})
					}
				});
			},
		}
	}
</script>

<style>
	.content {
		min-height: 100vh;
		background-color: #F2F2F2;
	}
</style>